/* -----------------------------------------------------------------------------------
 *
 *	Project Name:      ImagePreload svn.r3
 *	Author:            Matthias Seehauser - http://www.seesle.at
 *  Available since:   21.05.2009
 *	Last Modification: 21.05.2009
 *
 *	For more information, visit:
 *	http://www.seesle.at/projects/image-preload/
 *
 *	Licensed under the Creative Commons Attribution 3.0 License - http://creativecommons.org/licenses/by/3.0/
 *  	- Free for use in both personal and commercial projects
 *		- Attribution requires leaving author name, author link, and the license info intact.
 *	
 *  Version History:
 *    V1.0 (svn.r3): First Release; Class creation with PrototypeJS (>V1.6)
 */
var ImagePreload = Class.create({
  initialize: function( extImages, extFinished, extUpdate )
  {
    // load util function pointers
    this.intFinished = extFinished;
    this.intUpdate = extUpdate;
    
    // class variables
    this.procCount = 0;
    this.procPercentage = 0;
    this.intImages = new Array;
    this.intImageCount = extImages.length;
   
    // preload array of images
    for( var i = 0; i < extImages.length; i++ )
      this.Preload( extImages[i] );
  },
  Preload: function( extImage )
  {  
    var objImage = new Image;
    this.intImages.push( objImage );

    objImage.onload = this.OnFileComplete;

    objImage.refImagePreload = this; // give the image a reference to this class
    objImage.source = extImage;      // original source path to image (for later use)
    objImage.src = extImage;         // image object source path
  },
  OnFileComplete: function()
  { 
    // give the internal class reference to a shorter var.
    var classRef = this.refImagePreload; 
    
    // increment processed images counter
    classRef.procCount++;
    
    // calculate preload status
    classRef.procPercentage = Math.round( (classRef.procCount / classRef.intImageCount) * 100);
    
    // update percentage variable
    if( classRef.intUpdate ) classRef.intUpdate( classRef.procPercentage );
    
    // when the end is reached finish loading...
    if( classRef.procCount == classRef.intImageCount ) {
      classRef.procPercentage = 100;
      if( classRef.intFinished ) classRef.intFinished();
    }
  },
});